package servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Contact;
import model.ContactGroupManager;
import model.ContactNotFoundException;

/**
 * Servlet implementation class SeeContact
 */
public class SeeContact extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SeeContact() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String mode = (String)request.getParameter("mode");
		String name = (String)request.getParameter("name");
		
		Contact contact = null;
		try {
			contact = ContactGroupManager.getInstance().getContactByFullName(name);
		} catch (ContactNotFoundException e) {
			e.printStackTrace();
		}
		request.setAttribute("contact", contact);
		
		if("details".equals(mode))
			request.setAttribute("content", "details.jsp");
		else if("edit".equals(mode))
			request.setAttribute("content", "edit.jsp");
		request.getRequestDispatcher("jsp/main.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
